﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using IAC_2013.com.iac.form;
using IAC_2013.com.iac.util;

namespace IAC_2013.com.iac.action
{
    class BaseUnitAction
    {
        SqlConnection conn = new SqlConnection(DBUtils.CONNECTION_STRING);
        SqlCommand cmd;
        DataTable tb;
        SqlDataAdapter da;

        public Boolean addBaseUnit(BaseUnitForm frm)
        {
            try
            {
                string sql = "INSERT INTO dvcs_don_vi_co_so OUTPUT INSERTED.id_dvcs VALUES(";
                sql += "'"+ frm.baseUnitCode +"', ";
                sql += "N'"+ frm.baseUnitName +"', ";
                sql += "N'"+ frm.baseUnitAddress +"', ";
                sql += "'"+ frm.taxtCode +"', ";
                sql += "'"+ frm.accountCode +"', ";
                sql += "N'"+ frm.bankName +"', ";
                sql += "'"+ frm.phone +"', ";
                sql += "'"+ frm.email +"', ";
                sql += "'"+ frm.website +"')";

                cmd = new SqlCommand(sql, conn);
                conn.Open();
                frm.baseUnitId = (int)cmd.ExecuteScalar();

                string sqlWareHouse = "INSERT INTO kho_hang VALUES(";
                sqlWareHouse += "'"+ frm.warehouseCode +"', ";
                sqlWareHouse += "N'"+ frm.warehouseName +"', ";
                sqlWareHouse += "N'"+ frm.warehouseAddress +"', ";
                sqlWareHouse += "'"+ frm.warehousePhone +"', ";
                sqlWareHouse += "'"+ frm.warehouseFax +"', ";
                sqlWareHouse += frm.baseUnitId + ")";

                cmd = new SqlCommand(sqlWareHouse, conn);
                cmd.ExecuteNonQuery();

                conn.Close();
                cmd.Dispose();
                return true;

            }
            catch (Exception)
            {
                return false;
            }
        }

        public Boolean editBaseUnit(BaseUnitForm frm)
        {
            string sql = "update dvcs_don_vi_co_so set ";
            if (frm.baseUnitCode != null && !frm.baseUnitCode.Trim().Equals(""))
            {
                sql += " ma_dvcs = '"+ frm.baseUnitCode +"' ";
            }
            if (frm.baseUnitName != null && !frm.baseUnitName.Trim().Equals(""))
            {
                sql += " , ten_dvcs = N'" + frm.baseUnitName +"' ";
            }
            if (frm.baseUnitAddress != null && !frm.baseUnitAddress.Trim().Equals(""))
            {
                sql += " , dia_chi_dvcs = N'"+ frm.baseUnitAddress +"' ";
            }
            if (frm.taxtCode != null && !frm.taxtCode.Trim().Equals(""))
            {
                sql += " , ma_so_thue_dvcs = '"+ frm.taxtCode +"' ";
            }
            if (frm.accountCode != null && !frm.accountCode.Trim().Equals(""))
            {
                sql += " , so_tai_khoan_dvcs = '"+ frm.accountCode +"' ";
            }
            if (frm.bankName != null && !frm.bankName.Trim().Equals(""))
            {
                sql += " , ten_ngan_hang_dvcs = N'"+ frm.bankName +"' ";
            }
            if (frm.phone != null && !frm.phone.Trim().Equals(""))
            {
                sql += " , dien_thoai_dvcs = '"+ frm.phone +"' ";
            }
            if (frm.email != null && !frm.email.Trim().Equals(""))
            {
                sql += " , email_dvcs = '"+ frm.email +"' ";
            }
            if (frm.website != null && !frm.website.Trim().Equals(""))
            {
                sql += " , website_dvcs = '"+ frm.website +"' ";
            }
            sql += " where id_dvcs = " + frm.baseUnitId;
            
            try
            {
                cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                cmd.Dispose();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public Boolean deleteBaseUnit(BaseUnitForm frm)
        {
            string sql = "delete from dvcs_don_vi_co_so where id_dvcs = " + frm.baseUnitId;
            try
            {
                cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                cmd.Dispose();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable listBaseUnit(BaseUnitForm frm)
        {
            

            string sql = "select * from dvcs_don_vi_co_so where 1 = 1 ";
            if (frm.baseUnitCode != null && !frm.baseUnitCode.Trim().Equals(""))
            {
                sql += " and LOWER(ma_dvcs) LIKE '"+ "%" + frm.baseUnitCode + "%" +"' ";
            }
            if (frm.baseUnitName != null && !frm.baseUnitName.Trim().Equals(""))
            {
                sql += " and LOWER(ten_dvcs) LIKE '"+ "%" + frm.baseUnitName + "%" + "' ";
            }
            sql += " order by id_dvcs desc ";

            cmd = new SqlCommand(sql, conn);
            conn.Open();
            da = new SqlDataAdapter(cmd);
            tb = new DataTable();
            da.Fill(tb);
            conn.Close();
            cmd.Dispose();
            return tb;
        }
    }
}
